'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 建筑
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var buildConf = require('../config/buildConf');
var taskService = require('./taskService');
var buildType = require('../consts/buildType');
var buildTypeUtil = require('../util/buildTypeUtil');
var constantUtil = require('../util/constantUtil');
var dataApi = require('../libs/dataApi');
var fightPowerService = require('../services/fightPowerService.js');
var vipConf = require('../config/vipConf.js');

/**
 * 清除废墟
 * @param  {[type]} player_manager [description]
 * @return {[type]}                [description]
 */
exports.clearRuin = function (player_manager) {
    taskService.addClearRuinNum(player_manager);
};

/**
 * 升级建筑
 * @param  {[type]} player_manager [description]
 * @param  {[type]} build_type     [description]
 * @param  {[type]} build_lv       [description]
 * @return {[type]}                [description]
 */
exports.upgradeBuild = function (player_manager, build_type, build_lv) {
    /* jshint maxstatements:100 */
    if (!build_type) {
        return false;
    }
    switch (build_type) {
    case buildType.NEXUS: // 主城
        {
            taskService.addUpgradeNexusNum(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.MINE: // 金矿
        {
            taskService.addtaskMine(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.CHEST: // 金库
        {
            taskService.addtaskChest(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.FARM: // 农场
        {
            taskService.addtaskFarm(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.BARN: // 粮仓
        {
            taskService.addtaskBarn(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.PARADE: // 练兵场
        {
            taskService.addtaskParade(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.AIRSHIP: // 飞艇
        {
            taskService.addtaskAirship(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.SMITHY: // 铁匠铺
        {
            taskService.addtaskSmithy(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.WALL: // 城墙
        {
            taskService.addtaskWall(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.MARCHHALL: // 行军大厅
        {
            taskService.addtaskMarchhall(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.ATELIER: // 指挥中心
        {
            taskService.addtaskAtelier(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.HEROCOLLEGE: // 英雄学院
        {
            taskService.addtaskHerocollege(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.SOLDIERCOLLEGE: // 士兵学院
        {
            taskService.addtaskSoldiercollege(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.ACADEMY: // 研究院
        {
            taskService.addtaskAcademy(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.WISHTREE: // 许愿树
        {
            taskService.addtaskWishtree(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.BARRACK: // 兵营
        {
            taskService.addtaskBarrack(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.ARROW_TOWER: // 箭塔
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskDefense(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.CANNON_TOWER: // 炮塔
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskDefense(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.ICE_TOWER: // 魔法塔
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskDefense(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.LIGHT_TOWER: // 闪电塔
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskDefense(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.CROSSBOW_TOWER: // 投刃塔
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskDefense(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.DRAGON_TOWER: // 龙息塔
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskDefense(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.SNOW_TOWER: // 雪球塔
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskDefense(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.ELECTRIC_TOWER: // 磁暴塔
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskDefense(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.ROAD_BLOCK: // 路障
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskRoadBlock(player_manager, build_type, +build_lv);
            break;
        }
    case buildType.LANDMINE: // 地雷
        {
            taskService.addUnlockTower(player_manager, player_manager.build.getTowerNum());
            taskService.addtaskUpgradeRoad(player_manager, build_type, +build_lv);
            break;
        }
    }

    taskService.addBuildUpgrade(player_manager);
    return true;
};

/**
 * 判断建筑是否能升级
 * @param  {[type]}  build_model [description]
 * @param  {[type]}  build       [description]
 * @return {Boolean}             [description]
 */
exports.isBuildCanUpgrade = function (build_model, build) {
    var build_type = buildTypeUtil.getBuildType(build.getBuildId());
    var build_lv = build.getLv();

    var build_up_conf_list = dataApi.building_up_condition.findById(build_type * 100 + build_lv + 1);
    if (!build_up_conf_list) {
        return true;
    }

    var is_can_build = true;
    _.some(build_up_conf_list, function (build_up_conf) {
        var need_build = build_model.getBuildByType(build_up_conf.need_building);
        if (!need_build || need_build.getLv() < build_up_conf.level) {
            is_can_build = false;
            return true;
        }
    });

    return is_can_build;
};

/**
 * 直接升级消耗的钻石
 * @param  {[type]} second [description]
 * @return {[type]}        [description]
 */
exports.getDirectDiamond = function (second, vip_lv) {
    var add_vip_free_time = +(vipConf.getBuildFreeTime(vip_lv));
    var diamond_num =
        (second < 300 + add_vip_free_time) ? 0 :
        (second < 600 + add_vip_free_time) ? second / 30 :
        (second < 3600 + add_vip_free_time) ? second / 40 + 5 :
        (second < 36000 + add_vip_free_time) ? second / 60 + 35 :
        second / 120 + 335;
    return Math.ceil(diamond_num);
};

/**
 * 秒产兵消耗的钻石
 * @param  {[type]} second [description]
 * @return {[type]}        [description]
 */
exports.getDirectProduceDiamond = function (second) {
    var diamond_num =
        (second < 600) ? second / 30 :
        (second < 3600) ? second / 40 + 5 :
        (second < 36000) ? second / 60 + 35 :
        second / 120 + 335;

    return Math.ceil(diamond_num);
};

/**
 * 刷新建筑战力
 * @param  {[type]}   build_model      [description]
 * @return {[type]}             [description]
 */
exports.updateBuildPower = function (player_manager, update_single) {
    var build_model = player_manager.build;
    var build_power = 0;
    var build_list = [];
    _.each(build_model.eco_build_list, function (build_item) {
        var build_id = build_item.build_id;
        var build_lv = build_item.lv;
        if (build_lv > 0) {
            var build_type = buildTypeUtil.getBuildType(build_id);
            var build_conf = buildConf.getBuildLvConf(build_type, build_lv);
            if (!!build_conf) {
                build_power += build_conf.fp;
            }
        }
    });
    _.each(build_model.def_build_list, function (build_item) {
        var build_id = build_item.build_id;
        var build_lv = build_item.lv;
        var build_star = build_item.star;
        if (build_lv > 0) {
            var build_type = buildTypeUtil.getBuildType(build_id);
            var build_conf = buildConf.getBuildLvConf(build_type, build_lv);
            var build_star_conf = buildConf.getBuildStarConf(build_star);
            if (!!build_conf && !!build_star_conf) {
                build_power += build_conf.fp * Math.pow(1 + build_star_conf.value / 100, 2);
            }
        }
    });

    // 建筑战力
    build_model.build_power = Math.ceil(build_power);

    if (!update_single) {
        // 刷新总战力
        fightPowerService.updByIdMainPower(player_manager.getRoleId(), player_manager, function (err) {
            if (!err) {
                player_manager.save();
            }
        });
    }
};

exports.getBuildStarEff = function (build_item) {
    var build_star = build_item.star;
    var star_conf = buildConf.getBuildStarConf(build_star);
    if (!star_conf) {
        return;
    }
    var ratio = star_conf.value / 100;
    if (!!ratio) {
        var keys = ['min_atk', 'max_atk', 'fire_damage', 'ice_damage', 'light_damage', 'city_damage', 'hp'];
        _.each(keys, function (key) {
            build_item.attr[key] += Math.ceil(build_item.attr[key] * ratio);
        });
    }
};
